package zzs.generalframework.console.core.parameter.commonparameter.department.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.parameter.commonparameter.department.DepartmentBO;
import zzs.generalframework.common.core.parameter.commonparameter.department.DepartmentInfo;
import zzs.generalframework.common.core.parameter.commonparameter.department.DepartmentPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.parameter.commonparameter.department.dao.IDepartmentDao;
import zzs.generalframework.console.core.parameter.commonparameter.department.service.IDepartmentService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.department.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.parameter.commonparameter.department.service.impl.departmentServiceImpl")
public class DepartmentServiceImpl implements IDepartmentService
{

	private Logger logger = LoggerFactory.getLogger(DepartmentServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IDepartmentDao departmentDao;

	@Override
	public ConsoleReturnBO saveDepartment(DepartmentBO departmentBO)
	{
		logger.debug("====保存部门信息开始====");
		logger.debug("====保存部门信息.入参:DepartmentBO=[{}]====", departmentBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		DepartmentPO departmentPO = new DepartmentPO();
		copyDepartmentBO2DepartmentPO(departmentBO, departmentPO);
		Boolean insertResult = departmentDao.insert(departmentPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存部门信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存部门信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveDepartment(List<DepartmentBO> departmentBOList) throws ConsoleException
	{
		logger.info("====保存部门信息列表开始====");
		logger.debug("====保存部门信息列表.入参:DepartmentBOList=[{}]====", departmentBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (DepartmentBO itemBO : departmentBOList)
		{
			ConsoleReturnBO itemResult = saveDepartment(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存部门信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存部门信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getDepartment(DepartmentInfo departmentInfo)
	{
		logger.info("====获取部门信息开始====");
		logger.debug("====获取部门信息.入参:DepartmentInfo=[{}]====", departmentInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<DepartmentPO> departmentPOList = departmentDao.select(departmentInfo);
		if (departmentPOList == null || departmentPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (departmentPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			DepartmentBO departmentBO = new DepartmentBO();
			copyDepartmentPO2DepartmentBO(departmentPOList.get(0), departmentBO);
			returnConsoleReturnBO.setReturnObj(departmentBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			DepartmentBO departmentBO = new DepartmentBO();
			copyDepartmentPO2DepartmentBO(departmentPOList.get(0), departmentBO);
			returnConsoleReturnBO.setReturnObj(departmentBO);
		}
		logger.debug("====获取部门信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取部门信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listDepartment(DepartmentInfo departmentInfo)
	{
		logger.info("====获取部门信息列表开始====");
		logger.debug("====获取部门信息列表.入参:DepartmentInfo=[{}]====", departmentInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<DepartmentPO> departmentPOList = departmentDao.select(departmentInfo);
		if (departmentPOList == null || departmentPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<DepartmentBO> resultList = new ArrayList<DepartmentBO>();
			for (DepartmentPO itemPO : departmentPOList)
			{
				DepartmentBO itemBO = new DepartmentBO();
				copyDepartmentPO2DepartmentBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取部门信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取部门信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateDepartment(DepartmentBO departmentBO)
	{
		logger.info("====更新部门信息开始====");
		logger.debug("====更新部门信息.入参:DepartmentBO=[{}]====", departmentBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		DepartmentPO departmentPO = new DepartmentPO();
		copyDepartmentBO2DepartmentPO(departmentBO, departmentPO);
		Boolean updateResult = departmentDao.update(departmentPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新部门信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新部门信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateDepartment(List<DepartmentBO> departmentBOList) throws ConsoleException
	{
		logger.info("====更新部门信息列表开始====");
		logger.debug("====更新部门信息列表.入参:List<DepartmentBO>=[{}]====", departmentBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (DepartmentBO itemBO : departmentBOList)
		{
			ConsoleReturnBO itemResult = updateDepartment(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新部门信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新部门信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeDepartment(DepartmentBO departmentBO)
	{
		logger.info("====删除部门信息开始====");
		logger.debug("====删除部门信息.入参:DepartmentBO＝[{}]======", departmentBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		DepartmentPO departmentPO = new DepartmentPO();
		copyDepartmentBO2DepartmentPO(departmentBO, departmentPO);
		Boolean deleteResult = departmentDao.delete(departmentPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除部门信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除部门信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeDepartment(List<DepartmentBO> departmentBOList) throws ConsoleException
	{
		logger.info("====删除部门信息列表开始====");
		logger.debug("====删除部门信息列表.入参:List<DepartmentBO>＝[{}]======", departmentBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (DepartmentBO itemBO : departmentBOList)
		{
			ConsoleReturnBO itemResult = removeDepartment(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除部门信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除部门信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countDepartment(DepartmentBO departmentBO)
	{
		logger.info("====统计部门信息开始====");
		logger.debug("====统计部门信息。入参：DepartmentBO＝[{}]======", departmentBO);
		Long returnLong = new Long(0);
		DepartmentPO departmentPO = new DepartmentPO();
		copyDepartmentBO2DepartmentPO(departmentBO, departmentPO);
		returnLong = departmentDao.count(departmentPO);
		logger.debug("====统计部门信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计部门信息结束====");
		return returnLong;
	}

	private void copyDepartmentBO2DepartmentPO(DepartmentBO departmentBO, DepartmentPO departmentPO)
	{
		beanUtils.copyBeanNotNull(departmentBO, departmentPO);
		if (departmentBO.getSysID() != null)
		{
			departmentPO.setSeqID(departmentBO.getSysID());
		}
	}

	private void copyDepartmentPO2DepartmentBO(DepartmentPO departmentPO, DepartmentBO departmentBO)
	{
		beanUtils.copyBeanNotNull(departmentPO, departmentBO);
		if (departmentPO.getSeqID() != null)
		{
			departmentBO.setSysID(departmentPO.getSeqID());
		}
	}

}
